package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;

	public class TreeMapControls extends Sprite
	{		
		private var variables:Array;
		
		private var dataManager:DataManager;
		private var canvas:Canvas;

		private var shelf:Shelf;
		private var dragging:Variable = null;
		private var switchIndex:Number = -1;
		
		private var once:Boolean = true;
		private var twice:Boolean = true;
		private var third:Boolean = false;
		
		public function TreeMapControls(dm:DataManager)
		{
			super();
			
			dataManager = dm;
			
			makeShelf();
			makeVariables();
		}
		
		public function shelfData():ShelfData
		{
			return shelf.currentData();
		}
		
		public function setCanvas(c:Canvas):void
		{
			canvas = c;
		}
		
		public function zoomIn(value:String):void
		{
			shelf.zoomIn(value);
		}
		
		public function zoomOut():void
		{
			shelf.zoomOut();
		}
		
		private function makeShelf():void
		{
			shelf = new Shelf();
			addChild(shelf);
		}
		
		private function makeVariables():void
		{
			variables = dataManager.getVariables();
			
			for each (var name:String in variables) {
				var v:Variable = new Variable(name, variables.indexOf(name));
				setMouseActions(v);
				addChild(v);
			}
		}
		
		private function setMouseActions(v:Variable):void
		{
			v.addEventListener(MouseEvent.MOUSE_DOWN,
				function(evt:Event):void {
					var v:Variable = (evt.currentTarget as Variable);
					if (v.isLocked()) return;
					setChildIndex(v, numChildren-1);
					var pos:Number = shelf.getVariablePosition(v);
					if (pos != -1) {
						switchIndex = pos;
						shelf.removeVariableAt(pos);
					}
					dragging = v;
				}
			);
				
			v.addEventListener(MouseEvent.MOUSE_UP,
				function(evt:Event):void {
					var v:Variable = (evt.currentTarget as Variable);
					if (v.isLocked()) return;
					if (!shelf.aboveShelf(v)) {
						v.reset();
						v.clearValue();
					} else if (!shelf.onShelf(v)) {
						shelf.addVariable(v, switchIndex);
					}
					shelf.updateShelf(canvas);
					dragging = null;
					
					/*
					if (third) {
						shelf.clearValue("GENDER");
					}
					if (!once && !twice) {
						shelf.clearValue("AGE");
						third = true;
					}
					if (once) {
						shelf.setValue("GENDER", "FEMALE");
					}
					if (!once && twice) {
						shelf.setValue("AGE", "24");
						twice = false;
					}
					once = false;
					*/
					//shelf.setValue("INCOME", "$15,000");
					//shelf.setValue("RACE", "CHINESE");
				}
			);
			
			v.addEventListener(MouseEvent.MOUSE_MOVE,
				function(evt:Event):void {
					var v:Variable = (evt.currentTarget as Variable);
					if (v == dragging && shelf.size()>0) {
						var shelfVars:Array = shelf.getVariables();
						for (var i:Number=shelfVars.length-1; i>=0; i--) {
							if ((shelfVars[i] as Variable).isLocked() && shelf.aboveShelf(v)) {
								switchIndex = i+1;
								break;
							} else if (i == 0 && v.x < shelfVars[i].x && shelf.aboveShelf(v)) {
								switchIndex = i;
								break;
							} else if (i > 0 && v.x < shelfVars[i].x && v.x > shelfVars[i-1].x) {
								switchIndex = i;
								break;
							} else if (v.x < Shelf.SHELF_X - Variable.BOX_WIDTH || v.x > shelfVars[shelfVars.length-1].x) {
								switchIndex = -1;
								break;
							}
						}
					}
				}
			);
			
			v.addEventListener(MouseEvent.CLICK,
				function(evt:Event):void {
					var v:Variable = (evt.currentTarget as Variable);
					if (v.isLocked()) {
						shelf.zoomTo(v, canvas);
					}
				}
			);
		}
	}
}
